import { AuthGuard } from '@nestjs/passport'
import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  UsePipes,
  ValidationPipe,
  UseFilters,
  UseGuards,
  Req,
  Query,
} from '@nestjs/common'
import { HttpExceptionFilter } from 'src/filter/httpRequestCatch'
import { BarService } from './bar.service'
import { CreateBarDto } from './dto/create-bar.dto'
import { UpdateBarDto } from './dto/update-bar.dto'
import { Request } from 'express'
import { ReqUser } from '../strategy/dto/strategy.dto'
@Controller('bar')
@UsePipes(new ValidationPipe())
@UseFilters(HttpExceptionFilter) //异常过滤器
export class BarController {
  constructor(private readonly barService: BarService) {}
  //创建贴吧
  @Post('createBar')
  @UseGuards(AuthGuard('jwt'))
  create(@Body() createBarDto: CreateBarDto) {
    return this.barService.create(createBarDto)
  }

  //获取贴吧列表
  @Get()
  async findBarList(@Query() query: { page: number; pagesize: number }) {
    return await this.barService.findAll(query, null)
  }

  @Get('barByToken')
  @UseGuards(AuthGuard('jwt'))
  async findBarListByToken(@Req() req: Request, @Query() query: { page: number; pagesize: number }) {
    const user = req.user as ReqUser
    return await this.barService.findAll(query, user._id)
  }
  //模糊搜索查找贴吧
  @Get('serch')
  async likeFind(@Query() query: { page: number; pagesize: number; name: string }) {
    return await this.barService.likeFind(query)
  }

  //更改贴吧名字 logo 描述
  @Patch(':id')
  @UseGuards(AuthGuard('jwt'))
  async update(@Param('id') id: string, @Body() updateBarDto: UpdateBarDto, @Req() req: Request) {
    const userID = (req.user as { _id: string; username: string; email: string; photo: string | null; createdAt: Date })
      ._id

    //判断是否是创建者
    const isCreate = await this.barService.verifyBarCreate(id, userID)
    if (isCreate) {
      return await this.barService.update(id, updateBarDto)
    } else {
      return {
        data: null,
        code: 202,
        message: '权限不足',
      }
    }
  }

  //删除贴吧
  @Delete(':id')
  @UseGuards(AuthGuard('jwt'))
  async remove(@Param('id') id: string, @Req() req: Request) {
    const userID = (req.user as ReqUser)._id
    //判断是否是创建者
    const isCreate = await this.barService.verifyBarCreate(id, userID)

    if (isCreate) {
      return await this.barService.remove(id)
    } else {
      return {
        data: null,
        code: 202,
        message: '权限不足',
      }
    }
  }

  /* 关注贴吧 */
  @Get('subBar/:barID')
  @UseGuards(AuthGuard('jwt'))
  async subBar(@Req() req: Request, @Param('barID') barID: string) {
    const userID = (req.user as ReqUser)._id
    return await this.barService.sub({ user: userID, bar: barID })
  }

  /* 获取贴吧详情(有token) */
  @Get('barInfo/:barID')
  @UseGuards(AuthGuard('jwt'))
  async getBarMsgByToken(@Param('barID') barID: string, @Req() req: Request) {
    const user = req.user as ReqUser
    return this.barService.getBarMsg(barID, user._id)
  }

  /* 获取贴吧详情(无token) */
  @Get('barInfoNotToken/:barID')
  async getBarMsgByNotToken(@Param('barID') barID: string) {
    return this.barService.getBarMsg(barID)
  }
}
